﻿//GradeBook.cs
//GradeBook using rectanglar array to store grades 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GradeBookTest2
{
    class GradeBook
    {
        private int[,] grades;// rectanglar array of student grades 

        //auto-implemented property CourseName
        public string CourseName { get; set; }

        //two-parameter constructor initialize
        //auto-implemented property CourseName and grades array
        public GradeBook(string name, int[,] gradesArray)
        {
            CourseName = name;
            grades = gradesArray;
        }//end GradeBook constructor

        //find highest mark
        public int GetMaximum()
        {
            int max = grades[0,0]; //assume grades[0,0] is the highest mark

            //loop through grades array 
            foreach (int grade in grades)
            {
                //if grade is greater than max, assign grade to max
                if (grade > max)
                {
                    max = grade;
                }//end if

            }//end for

            return max;
        }//end method GetMaximum

        //find lowest mark in grades
        public int GetMinimum()
        {
            int min = grades[0, 0];//assume grades[0,0] is the lowest mark

            //loop in  grades 
            foreach (int grade in grades)
            {
                //if grade is lower than min, assign grade to min 
                if (grade < min)
                {
                    min = grade;
                }//end if 

            }//end for

            return min;
        }//end method GetMinimum

        //determine average grade for each student
        public double GetAverage(int student)
        {
            int total = 0; //initialize total 
            int amount = grades.GetLength(1); // get the number of grades per student 

            //sum grades for one student
            for (int i = 0; i < amount; i++)
            {
                total += grades[student, i];
            }//end for

            return (double)total / amount;
        }//end method GetAverage

        //Display welcome message to GradeBook user
        public void DisplayMessage()
        {
            Console.WriteLine("Welcome to the GradeBook for {0}\n", CourseName);
        }//end method DisplayMessage

        //output the contents of grades array 
        public void OutputGrades()
        {
            //get the number of students and grades per student
            int rows = grades.GetLength(0);
            int columns = grades.GetLength(1);

            //output the header
            Console.WriteLine("The grades are:\n");
            Console.Write("            ");//align column heads

            //create a column heading for each of the test
            for (int i = 0; i < columns; i++)
            {
                Console.Write("Test {0}  ", i);
            }//end for

            Console.WriteLine("Average");

            //create rows and columns representing grades array
            for (int i = 0; i < rows; i++)
            {
                Console.Write("Student {0,2}", i);

                for (int j = 0; j < columns; j++)
                {
                    Console.Write("{0,8}", grades[i, j]);
                }//end for

                Console.WriteLine("{0,9:F}", GetAverage(i));
            }//end outer for
        }//end method OutputGrades 

        //output bar chart displaying grades distribution
        public void OutputBarChart()
        {
            int[] frequency = new int[11];//store frequency of grades in each range of 10 grades

            //for each grade, increase appropriate frequency
            foreach (int grade in grades)
            {
                ++frequency[grade / 10];
            }//end for

            //for each grade,display bar in chart 

            Console.WriteLine("Overall grades distribution:");

            for (int i = 0; i < frequency.Length; i++)
            {
                if (10 == i)
                    Console.Write("  100:");
                else 
                    Console.Write("{0:D2}-{1:D2}:", i * 10, i * 10 + 9); 

                for (int count = 0; count < frequency[i]; count++)
                {
                    Console.Write("*");
                }//end for

                Console.WriteLine();
            }//end outer for
        }//end method OutputBarChart
        
        //perform virious operations on the data
        public void ProcessGrades()
        {
            OutputGrades();
            Console.WriteLine();

            Console.WriteLine("Lowest grade in the grade book {0}\nHighest grade in the grade book {1}\n", 
                GetMinimum(), GetMaximum());

            OutputBarChart();
        }//end function ProcessGrades 
    }//end class GradeBook
}
